<template>
  <div class="dashboard-container">
    <div style="display: flex; flex-direction: row;">
      <el-upload ref="upload" action="/upload" :multiple="false" :limit="1" :before-upload="beforeUpload">
        <el-button slot="trigger" size="middle" type="primary">上传excel</el-button>
      </el-upload>
      <div style="width: 20px;"></div>
      <el-button size="middle" type="primary" @click="onExcuteClick" v-loading="isExcuting">执行</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%" v-loading="isLoading">
      <el-table-column prop="id" label="ID" width="120">
      </el-table-column>
      <el-table-column prop="phone" label="电话号码" width="140">
      </el-table-column>
      <el-table-column prop="params" label="Params" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="proxy" label="Proxy" width="200">
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
/**
 * <el-table-column prop="success" label="执行成功次数" width="120">
      </el-table-column>
      <el-table-column prop="fail" label="执行失败次数" width="120">
      </el-table-column>
 */
import * as XLSX from 'xlsx';
const axios = require('axios');
export default {
  name: 'Dashboard',
  data() {
    return {
      isLoading: false,
      tableData: [],
      isExcuting: false
    }
  },
  mounted() {
    this.isLoading = true
    this.request({
      url: '/api/timefarm/setup',
      method: 'get',
    }).then(resp => {
      const data = resp.data;
      this.tableData = data
    }).finally(() => {
      this.isLoading = false
    })

    this.refreshExcuteStatus()
    this.timer = setInterval(() => {
      this.refreshExcuteStatus()
    }, 1000 * 60);
  },
  destroyed() {
    clearInterval(this.timer)
  },
  methods: {
    refreshExcuteStatus() {
      this.request({
        url: '/api/timefarm/excute',
        method: 'get',
      }).then(resp => {
        this.isExcuting = resp.data
      })
    },
    readFile(file) {
      //文件读取
      return new Promise(resolve => {
        let reader = new FileReader();
        reader.readAsBinaryString(file); //以二进制的方式读取
        reader.onload = ev => {
          resolve(ev.target.result);
        };
      });
    },
    async createWalletFile(file) {
      let data = await this.readFile(file);
      let workbook = XLSX.read(data, { type: "binary", cellDates: true }); //解析二进制格式数据
      let worksheet = workbook.Sheets[workbook.SheetNames[0]]; //获取第一个Sheet
      let result = XLSX.utils.sheet_to_json(worksheet); //json数据格式
      return result
    },
    async beforeUpload(file) {
      let result = await this.createWalletFile(file)
      for (let index = 0; index < result.length; index++) {
        const element = result[index];
        let params = element.params;
        if (params == null || params.length == 0) {
          continue
        }
        if (params.startsWith('src=')) {
          params = params.split("#tgWebAppData=")[1]
          params = params.split("&tgWebAppVersion")[0]
          params = decodeURIComponent(params)
          element.params = params
        }
      }

      this.submitTableData(result)
      return false
    },
    submitTableData(tableData) {
      this.isLoading = true
      this.request({
        url: '/api/timefarm/setup',
        method: 'post',
        data: tableData
      }).then((resp) => {
        const data = resp.data;
        this.tableData = data
      }).finally(() => {
        this.isLoading = false
      });
    },
    async onExcuteClick() {
      // const tempToken = "query_id=AAERjxg6AwAAABGPGDqWO_0X&user=%7B%22id%22%3A7417138961%2C%22first_name%22%3A%22Leleene%22%2C%22last_name%22%3A%22Venkent%22%2C%22username%22%3A%22LeleeneVenkent%22%2C%22language_code%22%3A%22es%22%2C%22allows_write_to_pm%22%3Atrue%7D&auth_date=1719413032&hash=fb5b178c4c9e4bab5bf2edbdf82823dc515e207570aef0edd2f531a4b83a7b60";
      // const tempToken = "query_id%3DAAF4cQYjAwAAAHhxBiMGx2oI%26user%3D%7B%22id%22%3A7030075768%2C%22first_name%22%3A%22Lisa%22%2C%22last_name%22%3A%22Griffin%22%2C%22language_code%22%3A%22en%22%2C%22allows_write_to_pm%22%3Atrue%7D%26auth_date%3D1719543734%26hash%3Db11a99d519a9a983212997311e427384a97da082e9cabcb3dc2d3ceacc98568e&tgWebAppVersion=7.4&tgWebAppPlatform=weba&tgWebAppThemeParams={"bg_color"%3A"%23ffffff"%2C"text_color"%3A"%23000000"%2C"hint_color"%3A"%23707579"%2C"link_color"%3A"%233390ec"%2C"button_color"%3A"%233390ec"%2C"button_text_color"%3A"%23ffffff"%2C"secondary_bg_color"%3A"%23f4f4f5"%2C"header_bg_color"%3A"%23ffffff"%2C"accent_text_color"%3A"%233390ec"%2C"section_bg_color"%3A"%23ffffff"%2C"section_header_text_color"%3A"%23707579"%2C"subtitle_text_color"%3A"%23707579"%2C"destructive_text_color"%3A"%23e53935"}"
      // let tempToken = `src="https://tg-tap-miniapp.laborx.io/#tgWebAppData=query_id%3DAAHL3LwqAwAAAMvcvCqRG0cb%26user%3D%257B%2522id%2522%253A7159471307%252C%2522first_name%2522%253A%2522Casey%2522%252C%2522last_name%2522%253A%2522Warren%2522%252C%2522language_code%2522%253A%2522en%2522%252C%2522allows_write_to_pm%2522%253Atrue%257D%26auth_date%3D1719543867%26hash%3Dd037ec0ee952d3b9c4cbc5e4b5af2bb3eb63993d2bd183c46e5438c8b7387907&tgWebAppVersion=7.4&tgWebAppPlatform=weba&tgWebAppThemeParams=%7B%22bg_color%22%3A%22%23ffffff%22%2C%22text_color%22%3A%22%23000000%22%2C%22hint_color%22%3A%22%23707579%22%2C%22link_color%22%3A%22%233390ec%22%2C%22button_color%22%3A%22%233390ec%22%2C%22button_text_color%22%3A%22%23ffffff%22%2C%22secondary_bg_color%22%3A%22%23f4f4f5%22%2C%22header_bg_color%22%3A%22%23ffffff%22%2C%22accent_text_color%22%3A%22%233390ec%22%2C%22section_bg_color%22%3A%22%23ffffff%22%2C%22section_header_text_color%22%3A%22%23707579%22%2C%22subtitle_text_color%22%3A%22%23707579%22%2C%22destructive_text_color%22%3A%22%23e53935%22%7D"`;
      // tempToken = tempToken.split("#tgWebAppData=")[1]
      // tempToken = tempToken.split("&tgWebAppVersion")[0]
      // tempToken = decodeURIComponent(tempToken)
      // const tempToken = `query_id=AAEtwhwgAwAAAC3CHCDj6AUw&user=%7B%22id%22%3A6981206573%2C%22first_name%22%3A%22%D0%95%D0%B2%D0%B3%D0%B5%D0%BD%D0%B8%D1%8F%22%2C%22last_name%22%3A%22%D0%9C%D0%B8%D1%85%D0%B0%D0%B9%D0%BB%D0%BE%D0%B2%D0%B0%22%2C%22username%22%3A%22Eleanor6Wright9%22%2C%22language_code%22%3A%22en%22%2C%22allows_write_to_pm%22%3Atrue%7D&auth_date=1719543714&hash=93ac9d242d1ce053f26b40fa0c8a1e8bb959df69f6ee3e460b5e564602eca437`;
      // const resp = await axios.post('https://tg-bot-tap.laborx.io/api/v1/auth/validate-init', tempToken, {
      //   headers: {
      //     'accept': 'application/json, text/plain, */*',
      //     'content-type': 'text/plain;charset=UTF-8',
      //   }
      // })
      // console.log(resp)
      // const authToken = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MTk1ODQ3NTYsInR5cGUiOiJhY2Nlc3MiLCJpYXQiOjE3MTk0OTgzNTYsImF1ZCI6InRnLmFpLmpvYnMiLCJpc3MiOiJ0Zy5haS5qb2JzIiwic3ViIjoiNzQxNzEzODk2MSIsImp0aSI6ImRnb29lcWx4eGN4YjB5In0.MG4vHejjEWxZPDucH9J4p_ySgzwsVEGMuOl6dRmefiYSHZVxyjj4VorGikBikWVPlBoqBtGxwvDlbmrIyLe3yJANw56-fzlhQdA4e8L03yk5Jl9M1L6oGXKhPArvaVhMD614xp9lbLM1yuJKGg6sm6SYidqkqIkzdncXgYegXAY7IpdwLL3BR-DCqrYgiaqmEMWV3ZCRajncITY84DPVTtKK3suUboCW8oKxQRdMePDY8kKLYbnZf9vFJU-sWHZvZjOK6ev1BXbfRadwFUg1lrlFj-D3a80uiuCjMisT0_mDSK8WvKUMKNeWhGyk21QKKDb3ZgLnewiQRF69L59TdJ93DfYPUbzLOonCY_nkwxk58cSZmUabQNr6qI7OwSMWFSzPalZrjKAGPYhb38cNY6yUb3pOHKPT-icjlaYB_ZCxDffotjQwIFpAhT4GEHDIFIIBl9mNFSm_VDdRhHqv3DlK9L4tHDtPeFF5TmLmc52ZOVFbWuNjij5ZSeK39-bJVMAJ9V3LFy018CtPF5gK1f3wv5s40p0H1lG06y06RKM9bSYylMaoAUMAswhI2hpmwmxVa-sJY7dsTafSVPPXQvKnxPCKZded28WyemkIz56KxvIJp8lzErviyIxuO-PM9gePynmPp-bRoE2zYUFHCnP-4wRMtQBNhtxAdAJmzCI';
      // console.log(authToken)
      // // 获取info是否在挖矿
      // const infoResp = await axios.request('https://tg-bot-tap.laborx.io/api/v1/farming/info', {
      //   method: "get",
      //   headers: {
      //     'authorization': 'Bearer ' + authToken,
      //   }
      // })
      // console.log(infoResp)
      // const farmResp = await axios.request('https://tg-bot-tap.laborx.io/api/v1/farming/start', {
      //   method: 'post',
      //   headers: {
      //     'authorization': 'Bearer ' + authToken,
      //   }
      // })
      // console.log(farmResp)
      // return
      this.request({
        url: '/api/timefarm/excute',
        method: 'post',
      }).then(resp => {
        this.isExcuting = resp.data
      })
    }
  },
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
